
package com.hook3.pianke.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hook3.pianke.Adapter.RadioGridViewAdapter;
import com.hook3.pianke.Adapter.RadioListViewAdapter;
import com.hook3.pianke.Adapter.RadioViewPagerAdapter;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.activity.base.radiouserbasepager.RadioUserBaseInfoActivity;
import com.hook3.pianke.domain.RadioInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

import java.util.logging.LogRecord;

/**
 * 电台页面
 * Created by HOME on 2015/12/8.
 */
public class MusicActivity {
    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    public View mToolbarMic;//toolbar右侧的imc

    LinearLayout mListHeader;//给listView添加头布局

    LinearLayout mMusicLinearLayout;//添加listView布局
    ListView mMusicList;

    //给viewpager设置adapterr
    ViewPager mViewPagerShowBig;

    //给gridView 设置adapter
    GridView mMusicGrid;

    //电台所有信息
    RadioInfo radioInfos;
    private ArrayList<RadioInfo.RadioCauousel> carouselDatas;

    public MusicActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
        initData();

    }

    private void init() {
        //设置”我要当主播“样式
        this.toolbar.setTitle("电台");
        this.toolbar.setTitleTextColor(Color.WHITE);
        mToolbarMic = View.inflate(activity, R.layout.actionbar_mic, null);
        toolbar.addView(mToolbarMic);
        mToolbarMic.measure(0,0);
        int leftPadding = toolbar.getWidth() - toolbar.getChildAt(0).getWidth()-toolbar.getChildAt(1).getWidth() - mToolbarMic.getMeasuredWidth();
        mToolbarMic.setPadding(leftPadding,0,0,0);
        toolbar.setTag(mToolbarMic);
        //添加listView布局
        mMusicLinearLayout = (LinearLayout) View.inflate(activity, R.layout.fl_music_list, null);

        mMusicList = (ListView) mMusicLinearLayout.findViewById(R.id.lv_music_list);

        //给listView添加头布局
        mListHeader = (LinearLayout) View.inflate(activity, R.layout.ll_music_list_header, null);
        mMusicList.setAdapter(null);
        mMusicList.addHeaderView(mListHeader);

    }

    /**
     * 初始化数据
     */
    private void initData() {
        //给mToolbarMic添加点击监听
        mToolbarMic.setOnClickListener(new ToolbarMicOnClickListener());

        //给viewpager设置adapterr
        mViewPagerShowBig = (ViewPager) mListHeader.findViewById(R.id.vp_music_list_bigShow);

        //给gridview设置adapter
        mMusicGrid = (GridView) mListHeader.findViewById(R.id.gv_music_grid_bigShow);

        //请求网络数据
        getMusicRadioFromNet();

        //给gridview设置点击监听
        mMusicGrid.setOnItemClickListener(new GridOnItemClickListener());

        //给listview设置点击监听
        mMusicList.setOnItemClickListener(new ListOnItemClickListener());

    }

    /**
     * 请求网络数据
     */
    private void getMusicRadioFromNet() {
        x.Ext.init(activity.getApplication());
        RequestParams params = new RequestParams(Url.RADIO);
       // params.addQueryStringParameter("wd", "xUtils");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();

                radioInfos = gson.fromJson(result, RadioInfo.class);

                //给listview 设置adapter
                setViewBaseAdapter();

                //给gridview设置adapter
                setGridBaseAdapter();


                //给viewpager设置adapterr
                setViewPagerAdapter();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {
                Toast.makeText(x.app(), "onFinished", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * mToolbarMic我要当主播的监听器实现类
     */
    private class ToolbarMicOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            Toast.makeText(activity, "I want to be Mic", Toast.LENGTH_LONG).show();

        }
    }

    /**
     * 给gridview设置点击监听
     */
    private class GridOnItemClickListener implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(activity, RadioUserBaseInfoActivity.class);
            intent.putExtra("radioid",radioInfos.data.hotlist.get(position-1).radioid);
            intent.putExtra("title",radioInfos.data.hotlist.get(position - 1).title);
            activity.startActivity(intent);
        }
    }

    /**
     * 给listview设置点击监听
     */
    private class ListOnItemClickListener implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(activity, RadioUserBaseInfoActivity.class);
            intent.putExtra("radioid",radioInfos.data.alllist.get(position-1).radioid);
            intent.putExtra("title",radioInfos.data.alllist.get(position-1).title);
            activity.startActivity(intent);
        }
    }



    //给viewpager设置adapterr
    private void setViewPagerAdapter(){
        carouselDatas = radioInfos.data.carousel;
        mViewPagerShowBig.setAdapter(new RadioViewPagerAdapter(carouselDatas,activity));
        frameLayout.addView(mMusicLinearLayout);
    }

    //给listview 设置adapter
    ArrayList<RadioInfo.RadioAllList> allRadioList;
    private void setViewBaseAdapter(){
        allRadioList = radioInfos.data.alllist;
        mMusicList.setAdapter(new RadioListViewAdapter(allRadioList,activity));
    }

    //给gridview设置adapter
    private void setGridBaseAdapter(){
        ArrayList<RadioInfo.RadioHotlist> radioHotlists = radioInfos.data.hotlist;
        mMusicGrid.setAdapter(new RadioGridViewAdapter(radioHotlists,activity));
    }

}
